package com.realbrand.net.realbrandpay.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.realbrand.net.realbrandpay.R;
import com.realbrand.net.realbrandpay.bean.BillBean;
import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.NetworkPolicy;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Administrator on 2017/10/19.
 */

public class BillAdapter extends BaseAdapter {
    List<BillBean> list;
    Context context;
    LayoutInflater inflater;

    public BillAdapter(List<BillBean> list, Context context) {
        this.list = list;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.billlistview_item, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tv_account.setText(list.get(position).getUserAccount());
        viewHolder.tv_day.setText(list.get(position).getTradeTime().split(" ")[0]);
        viewHolder.tv_hour.setText(list.get(position).getTradeTime().split(" ")[1]);
        Picasso.with(context).load(list.get(position).getUserAvatar()).memoryPolicy(MemoryPolicy.NO_CACHE).networkPolicy(NetworkPolicy.NO_CACHE).into(viewHolder.photo);
        if (list.get(position).getDealType().equals("0") || list.get(position).getDealType().equals("1") || list.get(position).getDealType().equals("3")) {
            viewHolder.tv_price.setText("-" + list.get(position).getDealMoney());
        } else if (list.get(position).getDealType().equals("2")) {
            viewHolder.tv_price.setText("+" + list.get(position).getDealMoney());
        }
        return convertView;
    }

    class ViewHolder {
        private ImageView photo;
        private TextView tv_price;
        private TextView tv_account;
        private TextView tv_day;
        private TextView tv_hour;

        public ViewHolder(View view) {
            photo = (ImageView) view.findViewById(R.id.bill_iv_img);
            tv_price = (TextView) view.findViewById(R.id.bill_tv_price);
            tv_account = (TextView) view.findViewById(R.id.bill_tv_storeName);
            tv_day = (TextView) view.findViewById(R.id.bill_tv_day);
            tv_hour = (TextView) view.findViewById(R.id.bill_tv_hour);
        }
    }
}
